﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;
using System.Windows;
using System.Collections.ObjectModel;

namespace SkeletalAnimation3D
{
    public class SkeletonInfo : ObservableCollection<BoneInfo>
    {
        public BoneInfo Root
        {
            get
            {
                if (this.Count == 0)
                {
                    return null;
                }
                else
                {
                    return this[0];
                }
            }
        }
        
        public new BoneInfo this[int boneId]
        {
            get
            {
                if (this.Count == 0)
                {
                    return null;
                }
                else
                {
                    return this.Items[0].GetBone(boneId);
                }
            }
        }

        internal void SetBoneData(int id, string name, Point3D position, double size, Quaternion orientation)
        {
            SetBoneData(null, this.Root, id, name, position, size, orientation);
        }

        private void SetBoneData(BoneInfo parent, BoneInfo boneInfo, int id, string name, Point3D position, double size, Quaternion orientation)
        {
            if (boneInfo.Id == id)
            {
                boneInfo.Parent = parent;
                boneInfo.Name = name;
                boneInfo.Position = position;
                boneInfo.Size = size;
                boneInfo.Orientation = orientation;
            }
            else
            {
                if (boneInfo.Children != null && boneInfo.Children.Count > 0)
                {
                    for (int i = 0; i < boneInfo.Children.Count; i++)
                    {
                        SetBoneData(boneInfo, boneInfo.Children[i], id, name, position, size, orientation);
                    }
                }
            }
        }

        internal bool SetOrientation(int boneId, Quaternion orientation)
        {
            return this[0].SetOrientation(boneId, orientation);
        }
    }
}
